package com.starsray;

import com.starsray.entity.Employee;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;

@ApplicationScoped
@Path("employee")
public class EmployeeReactiveResource {


    @GET
    @Path("/{empNo}")
    @Produces
    public Uni<List<Employee>> get(@PathParam("empNo") int empNo) {
        return Employee.list("id", 1L);
    }


    @POST
    @Path("create")
    public Uni<Response> create(Employee employee) {
        return Panache.<Employee>withTransaction(employee::persist)
                .onItem()
                .transform(inserted -> Response.created(URI.create("/Employees/" + inserted.id))
                        .build());
    }


    @GET
    @Path("/{id}")
    @Produces
    public Uni<Employee> getSingle(@PathParam("id") Long id) {
        return Employee.findById(id);
    }
}
